package com.ssau.expert.gui.common;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Author: Sergey Saiyan
 * Created: 03.10.13 12:19
 */
public class LoginFrame extends JFrame {

    public static final String EXPERT = "expert";
    public static final String USER = "user";

    private JTextField loginField;
    private JButton loginButton;

    public LoginFrame() {
        initDisposition();
        initComponents();
        setTitle("Авторизация");
    }

    private void initDisposition() {
        setSize(new Dimension(380, 62));
        setLocationRelativeTo(null);
        setLayout(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });
    }

    private void initComponents() {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setSize(new Dimension(380, 25));

        loginField = new JTextField();
        loginField.setSize(new Dimension(190, 25));

        panel.add(loginField);

        loginButton = MyControls.createButton("Войти", 190, 0);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = loginField.getText();
                if (EXPERT.equals(text)) {
                    dispose();
                    Controller.get().showExpertFrame();
                } else if (USER.equals(text)) {
                    dispose();
                    Controller.get().showUserFrame();
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "Неизвестный пользователь");
                }
            }
        });

        panel.add(loginButton);

        add(panel);
    }

    private static final String NAME = "Авторизация";

    public void showFrame() {
        setName(NAME);
        setTitle(NAME);
        setVisible(true);
    }
}
